package cn.edu.ncut.cs.springboot.petmanagementsystem.controller;

import cn.edu.ncut.cs.springboot.petmanagementsystem.common.Result;
import cn.edu.ncut.cs.springboot.petmanagementsystem.pojo.Role;
import cn.edu.ncut.cs.springboot.petmanagementsystem.service.RoleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/role")
@Tag(name = "权限管理模块")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @Operation(summary = "获取角色列表")
    @GetMapping("/list")
    public Result<List<Role>> getRoleList() {
        try {
            List<Role> roles = roleService.getRoleList();
            return Result.success(roles);
        } catch (Exception e) {
            log.error("获取角色列表失败", e);
            return Result.error("查询失败: " + e.getMessage());
        }
    }

    @Operation(summary = "创建角色")
    @PostMapping("/create")
    public Result<Role> createRole(@RequestBody CreateRoleRequest request) {
        try {
            Role role = new Role();
            role.setRoleName(request.getRoleName());
            role.setRoleCode(request.getRoleCode());
            role.setDescription(request.getDescription());
            role.setStatus(request.getStatus() != null ? request.getStatus() : 1);
            role.setPermissions(request.getPermissions());

            boolean success = roleService.createRole(role);
            if (success) {
                return Result.success("创建成功", role);
            } else {
                return Result.error("创建失败");
            }
        } catch (IllegalArgumentException e) {
            return Result.error(e.getMessage());
        } catch (Exception e) {
            log.error("创建角色失败", e);
            return Result.error("创建失败: " + e.getMessage());
        }
    }

    @Operation(summary = "分配角色")
    @PostMapping("/assign")
    public Result<?> assignRole(@RequestBody AssignRoleRequest request) {
        try {
            boolean success = roleService.assignRoleToUser(request.getUserId(), request.getRoleId());
            if (success) {
                return Result.success("分配成功");
            } else {
                return Result.error("分配失败");
            }
        } catch (IllegalArgumentException e) {
            return Result.error(e.getMessage());
        } catch (Exception e) {
            log.error("分配角色失败", e);
            return Result.error("分配失败: " + e.getMessage());
        }
    }

    @Operation(summary = "获取用户角色列表")
    @GetMapping("/user/{userId}")
    public Result<List<Role>> getUserRoles(
            @Parameter(description = "用户ID") @PathVariable Long userId) {
        try {
            List<Role> roles = roleService.getUserRoles(userId);
            return Result.success(roles);
        } catch (Exception e) {
            log.error("获取用户角色失败", e);
            return Result.error("查询失败: " + e.getMessage());
        }
    }

    @Data
    public static class CreateRoleRequest {
        private String roleName;
        private String roleCode;
        private String description;
        private Integer status;
        private String permissions;
    }

    @Data
    public static class AssignRoleRequest {
        private Long userId;
        private Long roleId;
    }
}

